package com.qf.test;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.dao.UserDao;
import com.qf.pojo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ShenZhuoPing
 * @version V1.0
 * @Project Mybatis_plus_Demo
 * @Package com.qf.test
 * @Description:
 * @Date 2022/7/18 11:13
 */
@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:spring-mybatis.xml")
public class Mybatis_Plus {
    @Autowired
    private UserDao userDao;
//    测试方法
    @Test
    public void test01(){
//        用户集合=查询的方法
        List<User> users = userDao.selectList(null);
        for (User user : users) {
//            输出集合里的每一条数据
            System.out.println(user);
        }
    }

//    添加
    @Test
    public void test02(){
        User user=new User();
        user.setUserName("xiongmao");
        user.setName("熊猫");
        user.setPassword("123");
        user.setAge(100);
        user.setEmail("xiongmao@163.com");
        userDao.insert(user);
    }
//    更新
    @Test
    public void Update(){
        User user=new User();
        user.setId(1548910598303834116L);
        user.setUserName("老虎");
        userDao.updateById(user);
    }
    @Test
    public void update(){
        User user=new User();
        user.setAge(50);
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name","熊猫");
        userDao.update(user,queryWrapper);
    }
    @Test
    public void Delete(){
        userDao.deleteById(1548910598303834116L);
    }

    @Test
    public void delete(){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name","熊猫");
        userDao.delete(queryWrapper);
    }
//    根据id查询
    @Test
    public void SelectById(){
        User user = userDao.selectById(1);
        System.out.println("user = " + user);
    }
//   查询1条 可以是0条  超过1条报错
    @Test
    public void selectOne(){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",1);
        User user = userDao.selectOne(queryWrapper);
        System.out.println("user = " + user);
    }
    //查询总记录
    @Test
    public void selectCount(){

        Integer count = userDao.selectCount(null);
        System.out.println("count = " + count);
    }
//    通过条件查询获得集合
    @Test
    public void testSelectList(){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("password","123456");
        List<User> users = userDao.selectList(queryWrapper);
        for (User user : users) {
            System.out.println("user = " + user);
        }
    }
//    分页查询
    @Test
    public void testSelectPage(){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("password","123456");

        Page<User> page=new Page<>(1,2);
        IPage<User> userIPage = userDao.selectPage(page, queryWrapper);
        System.out.println("当前页"+userIPage.getCurrent());
        System.out.println("总页数"+userIPage.getPages());
        System.out.println("总记录数"+userIPage.getTotal());
        System.out.println("当前页展示的数据"+userIPage.getRecords());
        System.out.println("当前页条数"+userIPage.getSize());
    }
//    allEq(map)
    @Test
    public void testSelectallEq(){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        Map<String,Object> map=new HashMap<>();
        map.put("password","123456");
        map.put("user_name","baihu");
        queryWrapper.allEq(map);
        List<User> users = userDao.selectList(queryWrapper);
        for (User user : users) {
            System.out.println("user = " + user);
        }
    }
//    in 查询
    @Test
    public void testSelectEq() {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.in("age",18,19,20,21);
        List<User> users = userDao.selectList(queryWrapper);
        for (User user : users) {
            System.out.println("user = " + user);
        }
    }
//    模糊查询
    @Test
    public void testSelectLike() {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
//        queryWrapper.like("password","123");  %xx%
//        queryWrapper.likeLeft("password",6); %xx
//        %//
        queryWrapper.likeRight("password",1);
        List<User> users = userDao.selectList(queryWrapper);
        for (User user : users) {
            System.out.println("user = " + user);
        }
    }
//    排序查询
    @Test
    public void testSelectOrderBy(){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByAsc("age");
        queryWrapper.orderByDesc("id");
        List<User> users = userDao.selectList(queryWrapper);
        for (User user : users) {
            System.out.println("user = " + user);
        }
    }
    //不写默认and  编写or
    @Test
    public void testSelectOrAnd(){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name","青龙");
        queryWrapper.or();
        queryWrapper.gt("age",15);
        List<User> users = userDao.selectList(queryWrapper);
        for (User user : users) {
            System.out.println("user = " + user);
        }
    }

}
